# dict迭代
d = { 'a': 1, 'b': 2, 'c': 3 }

for k, v in d.items():
    print('k:', k, 'v:', v)

for v in d.values():
    print(v)

for k in d.keys():
    print(k)

# 字符串迭代
for char in 'abcdef':
    print(char)

# 判断是否是可迭代对象
from collections import Iterable
isinstance('abc', Iterable) # True
isinstance([1,2,3], Iterable) # True
isinstance(123, Iterable) # False

# 练习
# 请使用迭代查找一个list中最小和最大值，并返回一个tuple：
def findMinAndMax(L):
    if not isinstance(L, list):
        raise TypeError('不是列表数据')
    if len(L) == 0:
        return None, None
    max = L[0]
    min = L[0]
    for num in L:
        if num > max:
            max = num
        if num < min:
            min = num
    return min, max


# 测试
if findMinAndMax([]) != (None, None):
    print('测试失败!')
elif findMinAndMax([7]) != (7, 7):
    print('测试失败!')
elif findMinAndMax([7, 1]) != (1, 7):
    print('测试失败!')
elif findMinAndMax([7, 1, 3, 9, 5]) != (1, 9):
    print('测试失败!')
else:
    print('测试成功!')

